package demo.struts2;

import java.util.ArrayList;

import com.opensymphony.xwork2.ActionSupport;

import DAO.ControlDB;
import Entity.Contact;

public class PreviousAction extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	private String firstname;
    private String surname;
    private int pagenumber;
    private int totalpage;
    private boolean isactive;
    private int PAGE_SIZE=5;
    private int id;
    public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
    public boolean getIsactive() {
		return isactive;
	}

	public void setIsactive(boolean isactive) {
		this.isactive = isactive;
	}

	
    public int getTotalpage() {
		return totalpage;
	}

	public void setTotalpage(int totalpage) {
		this.totalpage = totalpage;
	}

	
    public int getPAGE_SIZE() {
		return PAGE_SIZE;
	}

	public void setPAGE_SIZE(int pAGE_SIZE) {
		PAGE_SIZE = pAGE_SIZE;
	}

	public int getPagenumber() {
		return pagenumber;
	}

	public void setPagenumber(int pagenumber) {
		this.pagenumber = pagenumber;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	ArrayList<Contact> listContact;
    
	public String getFirstname() {
		return firstname;
	}

	public ArrayList<Contact> getListContact() {
		return listContact;
	}

	public void setListContact(ArrayList<Contact> listContact) {
		this.listContact = listContact;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	@Override
	public String execute() throws Exception {
		ControlDB con=new ControlDB();
		System.out.println(isactive);
		if(firstname.equals("")&&surname.equals("")){
			return "error";
		}else{
		if(isactive==true){
			 totalpage=con.totalFields(firstname,surname);
			 if(pagenumber==1){
				 listContact= con.getSearchName(surname, firstname,pagenumber,PAGE_SIZE);
			 }else{
			 pagenumber--;
			listContact= con.getSearchName(surname, firstname,pagenumber,PAGE_SIZE);
			 }
		}
		else{
		 totalpage=con.totalFields(firstname,surname,isactive);
		 if(pagenumber==1){
			 listContact= con.getSearchName(surname, firstname,pagenumber,PAGE_SIZE,isactive);
		 
		 }else{
		 pagenumber--;
		listContact= con.getSearchName(surname, firstname,pagenumber,PAGE_SIZE,isactive);
		 }
		}
		}
		if (listContact==null){
			return "error";
		}else{
			return "success";
		}
	}
	
}